<!--
 * @Description: 告警联系人管理页面
-->
<template>
  <BaseMainSlot>
    <template v-slot:searchbox>
      <el-collapse v-model="activeName" accordion @change="handleChange">
        <el-collapse-item name="1">
          <template slot="title">
            <div class="select-btns table-btns" @click.stop="changeSelect()">
              <i class="el-icon-circle-check"></i>
              <p v-if="activeName==='1'">收起筛选</p>
              <p v-if="activeName===''">展开筛选</p>
            </div>
          </template>
          <div class="select-box-content">
            <div class="select-box-content-name">联系人：</div>
            <el-input
              v-model="searchList.alarmContactName"
              clearable
              placeholder="请输入联系人"
              @change="queryPageList()"
            />
          </div>
        </el-collapse-item>
      </el-collapse>
    </template>
    <template v-slot:btnsBar>
      <ActionBar
        :add-active="addActive"
        :del-active="delActive"
        @add="add"
        @del="del"
      />
    </template>
    <template v-slot:content>
      <el-table
        v-loading="loading"
        highlight-current-row
        :data="tableData"
        stripe
        border
        height="calc(100% - 50px)"
        :row-key="
          row => {
            return row.alarmContactId;
          }
        "
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column type="index" label="序号" width="100" />
        <el-table-column prop="alarmContactName" label="姓名" />
        <el-table-column prop="phone" label="手机号" />
        <el-table-column prop="weChat" label="微信" />
        <el-table-column prop="email" label="邮箱" />
        <el-table-column label="操作" width="300">
          <template v-if="scoped.row.alarmContactId" slot-scope="scoped">
            <div class="table-btns-box">
              <div class="table-btns edit-btns" @click="edit(scoped.row)">
                <i class="el-icon-edit-outline"></i>
                <p>修改</p>
              </div>
              <div class="table-btns del-btns" @click="delOne(scoped.row)">
                <i class="el-icon-delete"></i>
                <p>删除</p>
              </div>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <Pagination
        :page-size="pageSize"
        :current-page="currentPage"
        :total-size="totalSize"
        @changePageSize="changePageSize"
        @changeCurrentPage="changeCurrentPage"
      />
      <AddEditDialog
        ref="addEditDialog"
        :now-chance-stau="nowChanceStau"
        :title-text="titleText"
        :edit-object="editObject"
        :show.sync="showAddEditDialog"
        @closeDialog="closeDialog"
        @cancelDialog="cancel"
      />
    </template>
  </BaseMainSlot>
</template>

<script>
import AddEditDialog from "./components/add-edit-dialog.vue";
import ActionBar from "@/components/ActionBar";
import Pagination from "@/components/Pagination";
import alarmContactManagementApi from "@/api/monitoring-alarm/alarm-contact-management";
import BaseMainSlot from "@/components/BaseMainSlot/kong.vue";
import { mymixin } from "@/utils/mixin";
export default {
  name: "AlarmContactManage",
  components: { ActionBar, Pagination, BaseMainSlot, AddEditDialog },
  mixins: [mymixin],
  data() {
    return {
      // 搜索内容
      searchList: {},
      // 选中的数据项数组 alarmContactId组成 (此数组最多为一个)
      checkedIdList: [],
    };
  },
  watch: {},
  created() {
    // 获取表格数据
    this.queryPageList();
  },
  mounted() {},
  methods: {
    queryPageList() {
      this.loading = true;
      let params = {
        page: this.currentPage,
        size: this.pageSize,
      };
      if (
        this.searchList.alarmContactName &&
        this.searchList.alarmContactName.length > 0
      ) {
        let searchparams = {
          alarmContactName: this.searchList.alarmContactName,
        };
        alarmContactManagementApi.querybycondition(searchparams).then((res) => {
          this.loading = false;
          if (res.code === 1) {
            // this.$message.success('查询成功')
          } else {
            this.$message.error("查询失败");
          }
          let data = res.data;
          // 赋值总条数
          this.totalSize = data.length;
          // 赋值数组
          this.tableData = data;
          this.addblankmet();
          this.resetActionBar();
        });
      } else {
        alarmContactManagementApi.getlist(params).then((res) => {
          this.loading = false;
          if (res.code === 0) {
            this.$message.error("查询失败");
          }
          let data = res.data;
          // 赋值总条数
          this.totalSize = data.totalElements;
          // 赋值数组
          this.tableData = data.content;
          this.addblankmet();
          this.resetActionBar();
        });
      }
    },
    search() {
      this.currentPage = 1;
      this.queryPageList();
    },

    handleSelectionChange(list) {
      this.checkedIdList = [];
      let notvalue = true;
      this.checkedIdList = list.map((item) => {
        if (item.alarmContactId) {
          return item.alarmContactId;
        } else {
          notvalue = false;
          return "";
        }
      });
      if (notvalue === false) {
        this.$message.warning("不可选择空数据！");
        return;
      }
      if (this.checkedIdList.length > 0) this.delActive = true;
      else this.delActive = false;
    },
    // 单个删除
    delOne(data) {
      this.$confirm("此操作将永久删除该项, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        customClass: "myClass",
      })
        .then(() => {
          alarmContactManagementApi
            .dellist([data.alarmContactId])
            .then((res) => {
              if (res.code === 1) {
                this.$message.success("删除成功");
                this.queryPageList();
              } else {
                this.$message.error("删除失败");
              }
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 表格删除
    delmessage() {
      alarmContactManagementApi.dellist(this.checkedIdList).then((res) => {
        if (res.code === 1) {
          this.$message.success("删除成功");
          this.queryPageList();
        } else {
          this.$message.error("删除失败");
        }
      });
    },
    // 添加
    add() {
      this.nowChanceStau = "add";
      this.titleText = "添加";
      this.addActive = false;
      this.delActive = false;
      this.showAddEditDialog = true;
    },
    // 修改
    edit(row) {
      if (row.alarmContactId) {
        this.editObject = JSON.parse(JSON.stringify(row));
        this.nowChanceStau = "edit";
        this.titleText = "修改";
        this.addActive = false;
        this.delActive = false;
        this.showAddEditDialog = true;
      } else {
        this.editObject = {};
      }
    },
  },
};
</script>

<style scoped lang="scss">
</style>
